<?php

/**
 * Created by PhpStorm.
 * User: dapeng
 * Date: 2018/9/29
 * Time: 下午12:14
 */
class Student
{
    private  $pagenum = 10;

//访问:index.php?a=student&f=tt



    //访问:index.php?a=student&f=add&page=1;
    function add(){
        /*
        $db = new MyPDO();
        $name = $_POST["name"];
        $sex = $_POST["sex"];
        $no = $_POST["no"];
        $date = $_POST["date"];

//        echo $date;
//        die();
        $tel = $_POST["tel"];

        $sql = "insert into dc_student(stud_name,stud_sex,stud_id,birth_date,mobile) 
VALUES ('{$name}','{$sex}','{$no}','$date','$tel')";

        echo $sql;
        $res = $db->insert($sql);
        */
        $db= new DB();
        $res = $db->insert("dc_student",$_POST);

        if($res > 0){
            echo "添加成功";//跳转到列表
            echo "<meta http-equiv=\"refresh\" content=\"3;url=index.php?a=student&f=userlist\">";

        }else{
            echo "添加失败";
        }

    }


    //访问:index.php?a=student&f=userlist&page=1;
    function userlist(){
        $db = MyPDO::getInstance();
//获取页码
        $page = isset($_GET['page']) ? $_GET['page'] : 1;
//计算Limit的值
        $num = ($page - 1)*$this->pagenum;
//分页查询
        $sql = "select * from dc_student order by create_time desc limit $num,$this->pagenum";
        $students = $db->select($sql);
//计算总页数 = 总记录 / 每页数量
        $sql2 = "select count(stud_id) as cont from dc_student";
        $count = $db->select($sql2);
//总页数
        $count_page = ceil($count[0]['cont']/$this->pagenum);
        include "html_userlist.php";
    }


    //删除
    //访问:index.php?a=student&f=delete&id=xxx
    function delete(){

        $db = MyPDO::getInstance();
        $id =$_GET['id'];
        $sql = "DELETE FROM dc_student  WHERE stud_id = '$id';";
        $res = $db->delete($sql);
        if($res > 0){
            echo "删除成功";//跳转到列表
            echo "<meta http-equiv=\"refresh\" content=\"3;url=index.php?a=student&f=userlist\">";

        }else{
            echo "删除失败";
        }
    }

    /**  编辑功能 编辑有点麻烦
     * 一、查询数据并绑定edit
     * 1.1、在编辑的按钮上绑定要编辑的ID
     * 1.2、根据ID查询到编辑的记录
     * 1.3、将查询到的记录  填充到编辑页面中 输入框中
     *
     * 二、更新功能实现 update
     * 2.1 页面上将Form表单改为POST提交
     * 2.2 点击"提交"按钮,进入更新(update)功能
     * 2.3 更新(update)功能:使用$_POST获取到页面上的值
     * 2.4 组合update更新语句
     * 2.5 执行SQL语句
     * 2.6 判断执行结果,跳回到首页
     */

//     一、查询数据并绑定edit
    function edit(){

        $id = $_GET['id'];
        $db = new DB();
       $student = $db->getRow("dc_student","stud_id=$id");
        include "html_editStudent.php";

    }

//    二、更新功能实现 update
    function update(){
        $db = new DB();
        $res = $db->update("dc_student",$_POST,"stud_id=".$_POST['stud_id']);
        echo $res?"成功":"失败";
    }



}